var express = require('express');
var router = express.Router();
var goods = require('../models/goods');
var users = require('../models/users')
var Q = require('q');

var goodsData = require('../../imoocMall/mock/goods')
    /* GET home page. */
router.get('/list', function(req, res, next) {
    let page = parseInt(req.param('page'));
    let pagesize = parseInt(req.param('pagesize'))
    let sort = req.param('sort');
    let skip = (page - 1) * pagesize;
    let priceLevel = req.param('priceLevel')
    let priceGt = '';
    let priceLte = '';
    let params = {};
    if (priceLevel != 'All') {
        priceGt = JSON.parse(priceLevel).priceStart;
        priceLte = JSON.parse(priceLevel).priceEnd;
        params = {
            salePrice: {
                $gt: priceGt,
                $lte: priceLte,
            }
        }
    }
    //skip 默认跳过多少条 mongoose 的分页方法
    //limit 是拿多少条数据
    let modelgoods = goods.find(params).skip(skip).limit(pagesize); //这样已经查询到数据了 对于数据进行排序 以及分页
    modelgoods.sort({ 'salePrice': sort })
    modelgoods.exec((err, doc) => {
        if (err) {
            res.json({
                "status": 404,
                'msg': err.message
            })
        } else {
            res.json({
                "status": 200,
                'msg': 'success',
                'result': doc
            })
        }
    })
});

router.post('/cartList', function(req, res, next) {
    if (!req.cookies.userId) {
        res.json({
            status: 1,
            msg: '请先登录,否则无法加入到购物车中!',
            result: ''
        })
    } else {
        let userId = req.cookies.userId;
        let productId = req.body.productId;
        users.findOne({ userId: userId }, function(userErr, userDoc) {
            if (userErr) {
                res.json({
                    status: 1,
                    msg: userErr.message
                })
            } else {
                if (userDoc.cartList.length > 0) {
                    var temp = {
                        number: -1,
                        doc: ''
                    }
                    userDoc.cartList.forEach((element, i) => {
                        if (element.productId == productId) {
                            temp.number = i;
                            temp.doc = element;
                            return;
                        }
                    })
                    if (temp.number >= 0) {
                        console.log("查询到了数据")
                        temp.doc.productNum++;
                        userDoc.save(function(err) {
                            if (err) {
                                res.json({
                                    status: 1,
                                    msg: err.message,
                                    result: ""
                                })
                            } else {
                                res.json({
                                    status: 0,
                                    msg: '',
                                    result: ""
                                })
                            }
                        })
                        return;
                    } else {
                        console.log('没有查询的那条数据')
                        goods.findOne({ productId: productId }, function(err, doc) {
                            if (err) {
                                res.json({
                                    status: 1,
                                    msg: err.message,
                                    result: ""
                                })
                            } else {
                                if (doc) {
                                    doc.productNum = 1;
                                    doc.checked = 1;
                                    userDoc.cartList.push(doc);
                                    userDoc.save(function(err) {
                                        if (err) {
                                            res.json({
                                                status: 1,
                                                msg: err.message,
                                                result: ""
                                            })
                                        } else {
                                            res.json({
                                                status: 0,
                                                msg: '',
                                                result: ""
                                            })
                                        }
                                    })
                                }
                            }
                        })
                    }
                } else {
                    console.log("数据库没有数据")
                    goods.findOne({ productId: productId }, function(err, doc) {
                        if (err) {
                            res.json({
                                status: 1,
                                msg: err.message,
                                result: ''
                            })
                        } else {
                            if (doc) {
                                doc.productNum = 1;
                                doc.checked = 1;
                                userDoc.cartList.push(doc);
                                userDoc.save(function(err) {
                                    if (err) {
                                        res.json({
                                            status: 1,
                                            msg: err.message,
                                            result: ""
                                        })
                                    } else {
                                        res.json({
                                            status: 0,
                                            msg: '',
                                            result: ""
                                        })
                                    }
                                })
                            }
                        }
                    })
                }
            }
        })
    }
})
module.exports = router;